package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * @Author: 王渤
 * @Time: 11:47
 * @Description:
 *
 * 后台管理中心
 */
//@CrossOrigin
@RestController
@RequestMapping("admin/product")
//@Api
public class ManageController {

    @Resource
    private ManageService manageService;

    @ApiOperation(value = "获取一级分类")
    @GetMapping("getCategory1")
    public Result getCategory1(){
        List<BaseCategory1> baseCategory1List = manageService.getCategory1();
        return Result.ok(baseCategory1List);
    }
    @ApiOperation(value = "获取二级分类")
    @GetMapping("/getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable Long category1Id){
        List<BaseCategory2> baseCategory2List = manageService.getCategory2(category1Id);
        return Result.ok(baseCategory2List);
    }

    @ApiOperation(value = "获取三级分类")
    @GetMapping("getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable Long category2Id){
        List<BaseCategory3> baseCategory3List = manageService.getCategory3(category2Id);
        return Result.ok(baseCategory3List);
    }


    @ApiOperation(value = "根据分类Id获取平台属性数据")
    @GetMapping("attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result<List<BaseAttrInfo>> attrInfoList(@PathVariable("category1Id") Long category1Id,
                                                   @PathVariable("category2Id") Long category2Id,
                                                   @PathVariable("category3Id") Long category3Id){
        List<BaseAttrInfo> baseAttrInfoList = manageService.getAttrInfoList(category1Id,category2Id,category3Id);
        return Result.ok(baseAttrInfoList);
    }


    // rest风格开发 原则 ： Get 查询 POst添加  PUT 修改 Delete删除
    @ApiOperation(value = "添加和修改平台属性")
    @PostMapping("saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        manageService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }

    @ApiOperation(value = "品牌列表查询 分页")
    @GetMapping("baseTrademark/{page}/{limit}")
    public Result baseTrademark(
            @PathVariable Integer page,
            @PathVariable Integer limit){
        IPage iPage = manageService.baseTrademark(page,limit);
        return Result.ok(iPage);
    }


    @ApiOperation(value = "根据平台属性ID获取平台属性")
    @GetMapping("getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable Long attrId){
       List<BaseAttrValue> list = manageService.getAttrValueList(attrId);
       return Result.ok(list);
    }

    @ApiOperation(value = "获取商品集合分页查询")
    @GetMapping("{page}/{limit}")
    public Result getSpuList(
            @PathVariable Integer page,
            @PathVariable Integer limit,
            Long category3Id){
        IPage infoIPage = manageService.getSpuList(page,limit,category3Id);
        return Result.ok(infoIPage);
    }


    @ApiOperation(value = "查询所有品牌列表")
    @GetMapping("/baseTrademark/getTrademarkList")
    public Result getTrademarkList(){
        List<BaseTrademark> baseTrademarkList =  manageService.getTrademarkList();
        return Result.ok(baseTrademarkList);
    }

    @ApiOperation(value = "查询销售属性列表")
    @GetMapping("baseSaleAttrList")
    public Result baseSaleAttrList(){
        List<BaseSaleAttr> baseSaleAttrList = manageService.baseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }

    //添加spu
    @ApiOperation(value = "添加spu")
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    @ApiOperation(value = "查询SKU分页")
    @GetMapping("list/{page}/{limit}")
    public Result getSkuList(
            @PathVariable Integer page,
            @PathVariable Integer limit){
        IPage iPage = manageService.getSkuList(page,limit);
        return Result.ok(iPage);
    }


    @ApiOperation(value = "根据spuId获取图片列表")
    @GetMapping("spuImageList/{spuId}")
    public Result spuImageList(
            @PathVariable Long spuId){
        List<SpuImage> imageList = manageService.spuImageList(spuId);
        return Result.ok(imageList);
    }

    @ApiOperation(value = "根据spuId获取销售属性")
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(
            @PathVariable Long spuId){
        List<SpuSaleAttr> spuSaleAttrList = manageService.spuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }

    @ApiOperation(value = "添加sku")
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        manageService.saveSkuInfo(skuInfo);
        return Result.ok();
    }


    @ApiOperation(value = "上架")
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId){
        manageService.onSale(skuId);
        return Result.ok();
    }

    @ApiOperation(value = "下架")
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        manageService.cancelSale(skuId);
        return Result.ok();
    }
}
